﻿var userlist = {
    formDefine: {
        labelWidth: 30,
        fields: [
                    { display: "所属空间", name: "ORGANIZATION_ID", type: "select", labelWidth: 50, inputWidth: 170 },
                    { display: "查询", name: "search", type: "button", click: function () { userlist.search(); } },
                ],
        fieldsInit: function () {
            loadOrgData();
        }
    },
    gridDefine: {
        columns: [

            { display: '文件名称', name: 'USER_NAME', minWidth: 90 },
            { display: '所属空间', name: 'USER_ACCOUNT', minWidth: 80 },
            {display: '部门', name: 'ORGANIZATION_NAME', minWidth: 200 },
            { display: '角色', name: 'ROLES_NAMELIST', minWidth: 200 },
            { display: '空间状态', name: 'SPACE_STATUS', minWidth: 60, align: 'center', render: LinkSpaceStatus }
        ],
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryUserInfoPageList"
    },
    init: function () {
        jsPageRender(this, true);

    },
    search: function () {
        jsPageSearch(this);
    }
};

function LinkOP(value, row, index) {
    var _str = 'userid=' + row.USER_ID, spacestatus = '';

    if (row.SPACE_STATUS == "1") {
        spacestatus = '<a href="javascript:void(0)" onclick="handlerspaceStatus(\'' + row.USER_ID + '\',0);">锁定</a>';
    } else {
        spacestatus = '<a href="javascript:void(0)" onclick="handlerspaceStatus(\'' + row.USER_ID + '\',1);"><span style="color:red">解锁</span></a>';
    }
    spacestatus += '<span style="padding-left:15px;"><a href="javascript:void(0)" onclick="resetpassword(\'' + row.USER_ID + '\',0);">重置密码</a></span>';
    return '<span style="padding-right:15px;"><a href="javascript:void(0)" onclick="openPage(\'useredit\',\'' + _str + '\');">编辑</a></span><span style="padding-right:15px;"><a href="javascript:void(0)" onclick="deleteuser(\'' + row.USER_ID + '\',\'' + row.USER_NAME + '\');">删除</a></span>' + spacestatus;
}



//#region 加载营业部数据
function loadOrgData(_currentval) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryOrganizeList",
        success: function (data) {
            if (data && data.length > 0) {
                var _dataarr = [];
                var rootobj = {};
                rootobj.id = '';
                rootobj.text = '全部';
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ORGANIZATION_ID;

                        obj.text = data[i].ORGANIZATION_NAME;
                        getsubOrg(data, data[i].ORGANIZATION_ID, obj);
                        _data.push(obj);
                    }
                }
                rootobj.children = _data;
                _dataarr.push(rootobj);
                $('#formidORGANIZATION_ID').combotree({ multiple: false });
                $('#formidORGANIZATION_ID').combotree('loadData', _dataarr);
                //                if (_currentval&&_currentval != '') {
                //                    $('#formidORGANIZATION_ID').combotree('setValue', _currentval);
                //                }

            }

        }
    });

    function getsubOrg(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ORGANIZATION_ID;
                    obj.text = _d[i].ORGANIZATION_NAME;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubOrg(_d, _sdata[j].ORGANIZATION_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

//#region 删除
function deleteuser(userid, username) {
    $.extend($.messager.defaults, {
        ok: "确定",
        cancel: "取消"
    });
    $.confirm("确定删除当前用户[" + username + "]", "删除用户", function (b) {
        if (b) {
            $.ajaxHandler({
                url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
                urlType: "EditUserInfo",
                model: function () { return $.toJSON({ 'action': 0, 'USER_ID': userid }); },
                success: function (data) {
                    $.alert('删除成功', null, function () { userlist.search(); });
                }
            });
        }
    });
}
//#endregion

